package com.study.notepad.util;/*
 @Author Cedar
 @DESCRIPTION 
 @Date 2019/10/24 14:44
*/

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

public class ExcelUtil {


    public static Workbook createNoteExcel(List<List<String>> dataList) {
        String[] title = new String[]{"ID", "标题", "内容", "创建时间", "修改时间"};
        String[] head = new String[]{"笔记列表"};

        String sheetName = "笔记";

        Workbook wb = new HSSFWorkbook();//创建工作簿
        Sheet sh = wb.createSheet(sheetName);//创建一个sheet页
        sh.setDefaultRowHeight((short) 700);
        sh.setDefaultColumnWidth((short) 20);
        sh.setColumnWidth(2, (short) 60 * 256);
        sh.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        Row headRow = sh.createRow(0);//创建一行

        headRow.setHeight((short) 300);
        for (int i = 0; i < head.length; i++) {
            Font font = wb.createFont();
            font.setFontHeightInPoints((short) 10);//设置字体高度
            font.setColor(IndexedColors.BLACK.getIndex());
            font.setFontName("微软雅黑");
            font.setBold(true);

            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格水平方向对齐方式
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置单元格垂直方向对齐方式
            if (i == 1) {
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //填充单元格
                cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
                font.setColor(IndexedColors.WHITE.getIndex());
            }
            cellStyle.setFont(font);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);

            Cell cell = headRow.createCell(i * 8);//创建一个单元格
            cell.setCellStyle(cellStyle);//设置单元格样式
            cell.setCellValue(head[i]);//设置值
        }
        Row titleRow = sh.createRow(1);//创建一行
        titleRow.setHeight((short) 700);
        for (int i = 0; i < title.length; i++) {
            Font font = wb.createFont();
            font.setFontHeightInPoints((short) 10);//设置字体高度
            font.setColor(IndexedColors.WHITE.getIndex());
            font.setFontName("微软雅黑");
            font.setBold(true);

            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格水平方向对齐方式
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置单元格垂直方向对齐方式
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //填充单元格
            cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
            cellStyle.setFont(font);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);

            Cell cell = titleRow.createCell(i);//创建一个单元格
            cell.setCellStyle(cellStyle);//设置单元格样式
            cell.setCellValue(title[i]);//设置值
        }
        int lastRowNum = sh.getLastRowNum();
        for (int i = 0; i < dataList.size(); i++) {
            Row row = sh.createRow(i + lastRowNum + 1);//创建一行
            row.setHeight((short) 700);
            List<String> list3 = dataList.get(i);
            for (int j = 0; j < list3.size(); j++) {
                Font font = wb.createFont();
                font.setFontHeightInPoints((short) 10);//设置字体高度
                font.setColor(IndexedColors.RED.getIndex());
                font.setFontName("微软雅黑");

                CellStyle cellStyle = wb.createCellStyle();
                cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格水平方向对齐方式
                cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置单元格垂直方向对齐方式
                cellStyle.setBorderTop(BorderStyle.THIN);
                cellStyle.setBorderBottom(BorderStyle.THIN);
                cellStyle.setBorderLeft(BorderStyle.THIN);
                cellStyle.setBorderRight(BorderStyle.THIN);
                cellStyle.setFont(font);

                Cell cell = row.createCell(j);//创建一个单元格
                cell.setCellValue(list3.get(j));//设置值
                cell.setCellStyle(cellStyle);
            }
        }
        return wb;
    }

}
